//
//  Currency.swift
//  LOTR Converter
//
//  Created by li on 2025/4/2.
//
import SwiftUI
enum Currency: Double,CaseIterable,Identifiable{

    
    case copperPenny = 6400
    case sliverPenny = 64
    case sliverPiece = 16
    case goldPenny = 4
    case goldPiece = 1
    
    func convert(amount:String,currency:Currency) -> String {
        
        guard let doubleAmount = Double(amount) else{
            return ""
        }    //guard: try to do,else在发生Exception情况
        
        let convertedAmount = doubleAmount / self.rawValue * currency.rawValue
        
        
        return String(format: "%.2f", convertedAmount)//保留两位小数
    }
    
    
    var id: Currency{
        self
    }
    
    var image: ImageResource{
        switch self{ //self实例
        case .copperPenny:
                .copperpenny
        case .sliverPiece:
                .silverpiece
        case .sliverPenny:
                .silverpenny
        case .goldPenny:
                .goldpenny
        case .goldPiece:
                .goldpiece
        }
    }
    
    var name:String{
        switch self {
        case .copperPenny:
            "铜便士"
        case .sliverPenny:
            "银便士"
        case .sliverPiece:
            "银币"
        case .goldPenny:
            "金便士"
        case .goldPiece:
            "金币"
        }
    }
}
